package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyCommend")
public class StrategyCommentController {

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Value("${file.dir}")
    private String dir;

    //http://localhost:81/strategyCommend/list
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model){
        PageInfo<StrategyCommend> pageInfo = strategyCommendService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyCommend/list";
    }

    //http://localhost:81/strategyCommend/save
    /*
        detailId: 3
        title: 1111232211111
        subTitle: 232323232323
        pic: (binary)
        sequence: 6
        state: 0
    */
    @RequestMapping("/save")
    @ResponseBody
    public Object save(StrategyCommend entity, MultipartFile pic) {
        if (pic != null && pic.getSize() > 0) {
            String coverUrl = UploadUtil.upload(pic, dir);
            entity.setCoverUrl("/"+coverUrl);
        }

        strategyCommendService.save(entity);
        return new AjaxResult();
    }
}
